package me.mystream.streamdemo;

import java.util.ArrayList;

public class StreamDemo1 {
    public static void main(String[] args) {
        //创建集合添加元素
      /*  1.把所有以张开头的元素储存到新集合中
            2.把张开头的，长度为3的元素再存储到新集合中*/

        //方式1，繁琐
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("张无忌");
        list1.add("张强");
        list1.add("张三丰");
        list1.add("李四");

        ArrayList<String> list2 = new ArrayList<>();
        for (String name : list1) {
            if (name.startsWith("张")) {
                list2.add(name);
            }
        }

        ArrayList<String> list3 = new ArrayList<>();
        for (String name : list2) {
            if (name.length() == 3) {
                list3.add(name);
            }
        }
        System.out.println(list3);

        //方式2，stream流
        list1.stream().filter(name -> name.startsWith("张")).filter(name -> name.length() == 3).forEach(name -> System.out.println(name));
    }
}
